package model

const collection string = "book"

type Book struct {
	Id int `bson:"_id"`
	Name string `bson:"name"`
	Catory string `bson:"catory"`
	Money float32 `bson:"money"`
}

func (self *Book) Save() (int64,error) {
	session := mongoSession.Copy()
	defer session.Close()

	mc := session.DB(DB).C(collection)
	err := mc.Insert(self)
	if err != nil {
		return -1,err
	}
	return 1,nil
}

func (self *Book) GetById() (*Book,error)  {
	session := mongoSession.Copy()
	defer session.Close()

	mc := session.DB(DB).C(collection)

	var book Book
	param := make(map[string]interface{})
	param["_id"] = self.Id
	err := mc.Find(param).One(&book)
	return &book,err
}
